unit FMVNC_;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ProcessManager;

type
  TFmVNC = class(TForm)
    Label1: TLabel;
    TmVNC: TTimer;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TmVNCTimer(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    FProcessCheckCount: Integer;
    FProcessManager : TProcessManager;
  public

  end;


implementation

const
  VNC_PROCESS_NAME = 'maintain.exe';

{$R *.dfm}

procedure TFmVNC.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  TmVnc.Enabled := False;
  FProcessManager.Free;
  FProcessManager := nil;
  Action := caFree;
end;

procedure TFmVNC.FormCreate(Sender: TObject);
begin
  SetWindowLong(Handle, GWL_STYLE, GetWindowLong(Handle, GWL_STYLE) And Not WS_CAPTION);
  FProcessManager := TProcessManager.Create(self);
end;

procedure TFmVNC.FormShow(Sender: TObject);
begin
  Tmvnc.Enabled := True;
end;

procedure TFmVNC.TmVNCTimer(Sender: TObject);
begin
  TmVnc.Enabled := False;
  FProcessManager.Open;
  if not FProcessManager.ExistProcess(VNC_PROCESS_NAME) then begin
    FProcessCheckCount := FProcessCheckCount + 1; 
    if FProcessCheckCount >= 2 then begin
      TmVNC.Enabled := False;
      PostMessage(Self.Handle, wm_close, 0, 0);
      Exit;    
    end;
  end;
  TmVnc.Enabled := True;
end;

end.
